package com.zhengf.rdf.web;


import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zhengf.rdf.annotation.UserLoginToken;
import com.zhengf.rdf.entity.User;
import com.zhengf.rdf.service.UserService;
import com.zhengf.rdf.utis.ToKenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengf
 * @since 2019-01-23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userSer;

    /**
     * 查询当前人员信息
     * @param token
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "/query")
    public JSONObject queryUser(@RequestHeader("token") String token) {
        JSONObject jsonObject=new JSONObject();
        String username = JWT.decode(token).getAudience().get(0);
        User user = new User();
        user.setUserName(username);
        EntityWrapper ew = new EntityWrapper();


        List list = userSer.selectList(ew);
        if(list.size() == 0){
            // 失败
            jsonObject.put("status", "false");
            jsonObject.put("message","登录失败,用户名或密码错误");
            return jsonObject;
        }else {
            jsonObject.put("user", list.get(0));
            jsonObject.put("message", "true");
            jsonObject.put("status", "true");

            return jsonObject;
        }
    }

    /**
     * 查询通讯录
     * @param token
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "/queryAll")
    public JSONObject queryAll(@RequestHeader("token") String token) {
        JSONObject jsonObject=new JSONObject();
        User user = new User();
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(user);
        ew.orderBy("id",false);
        List<User> users = userSer.selectList(ew);

        if(users.size() == 0){
            // 失败
            jsonObject.put("status", "false");
            jsonObject.put("message","查询通讯录");
            return jsonObject;
        }else {
            jsonObject.put("user", users);
            jsonObject.put("message", "true");
            jsonObject.put("status", "true");

            return jsonObject;
        }
    }

    /**
     * 查询id
     * @param token
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "/queryID")
    public JSONObject queryID(@RequestHeader("token") String token,
                              @RequestParam("id") String id) {
        JSONObject jsonObject=new JSONObject();
        User user = userSer.selectById(id);
        if(user == null){
            // 失败
            jsonObject.put("status", "false");
            jsonObject.put("message","查询通讯录失败");
            return jsonObject;
        }else {
            jsonObject.put("user", user);
            jsonObject.put("message", "查询通讯录成功");
            jsonObject.put("status", "true");

            return jsonObject;
        }
    }

    /**
     * 新增用户
     * @param token
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "/insert")
    public JSONObject insert(@RequestHeader("token") String token,
                             @RequestParam("userName") String userName,
                             @RequestParam(value = "password",required = false) String password,
                             @RequestParam("gender") String gender,
                             @RequestParam("birthday") String birthday,
                             @RequestParam("email") String email,
                             @RequestParam("phone") String phone,
                             @RequestParam("landline") String landline,
                             @RequestParam("depa") String depa,
                             @RequestParam("post") String post,
                             @RequestParam("organization") String organization,
                             @RequestParam("autograph") String autograph) throws ParseException {
        JSONObject jsonObject=new JSONObject();

        User user = new User();
        user.setUserName(userName);
        if(password != null){
            user.setPassword(password);
        }else {
            user.setPassword("123123");
        }
        user.setGender(gender);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");

        user.setBirthday(sdf.parse(birthday));
        user.setEmail(email);
        user.setPhone(phone);
        user.setLandline(landline);
        user.setDepa(depa);
        user.setPost(post);
        user.setOrganization(organization);
        user.setAutograph(autograph);

        boolean insert = userSer.insert(user);


        if(insert == false){
            // 失败
            jsonObject.put("status", "false");
            jsonObject.put("message","插入用户失败");
            return jsonObject;
        }else {
            jsonObject.put("message", "插入用户成功");
            jsonObject.put("status", "true");

            return jsonObject;
        }
    }


    /**
     * 根据id更新
     * @param token
     * @param id
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "updateID")
    private JSONObject updateID(@RequestHeader("token") String token,
                                @RequestParam(value = "id") String id ,
                                @RequestParam("userName") String userName,
                                @RequestParam(value = "password",required = false) String password,
                                @RequestParam("gender") String gender,
                                @RequestParam("birthday") String birthday,
                                @RequestParam("email") String email,
                                @RequestParam("phone") String phone,
                                @RequestParam("landline") String landline,
                                @RequestParam("depa") String depa,
                                @RequestParam("post") String post,
                                @RequestParam("organization") String organization,
                                @RequestParam("autograph") String autograph) throws ParseException {

        User user = new User();
//        user.setUserName(userName);
        if(password != null){
            user.setPassword(password);
        }else {
            user.setPassword("123123");
        }
        user.setGender(gender);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");

        user.setBirthday(sdf.parse(birthday));
        user.setEmail(email);
        user.setPhone(phone);
        user.setLandline(landline);
        user.setDepa(depa);
        user.setPost(post);
        user.setOrganization(organization);
        user.setAutograph(autograph);

        user.setId(Integer.parseInt(id));

        boolean b = userSer.updateById(user);
        JSONObject jsonObject=new JSONObject();
        if(b == false){
            jsonObject.put("status", "false");
            jsonObject.put("message","更新个人信息失败");
            return jsonObject;

        }else {
            jsonObject.put("status", "true");
            jsonObject.put("message","更新个人信息成功");
            return jsonObject;
        }
    }
    /**
     * 查询toKen
     * @param token
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "/queryToken")
    public JSONObject queryToken(@RequestHeader("token") String token) {
        String username = ToKenUtils.getUsername(token);
        JSONObject jsonObject=new JSONObject();
        User user = new User();
        user.setUserName(username);
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(user);
        List<User> list = userSer.selectList(ew);

        if(list.size() == 0){
            // 失败
            jsonObject.put("status", "false");
            jsonObject.put("message","查询通讯录失败");
            return jsonObject;
        }else {
            User o = list.get(0);
            jsonObject.put("user", o);
            jsonObject.put("message", "查询通讯录成功");
            jsonObject.put("status", "true");

            return jsonObject;
        }
    }
    /**
     * 查询id
     * @param token
     * @return
     */
    @UserLoginToken
    @PostMapping(value = "/deleteID")
    public JSONObject deleteID(@RequestHeader("token") String token,
                               @RequestParam("id") Integer id) {
        JSONObject jsonObject=new JSONObject();
        boolean b = userSer.deleteById(id);
        if(b == false){
            // 失败
            jsonObject.put("status", "false");
            jsonObject.put("message","删除通讯录失败");
            return jsonObject;
        }else {
            jsonObject.put("message", "删除通讯录成功");
            jsonObject.put("status", "true");

            return jsonObject;
        }
    }
}
